package com.masteringwave;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;

import net.bican.wordpress.Page;
import net.bican.wordpress.PageDefinition;
import net.bican.wordpress.Wordpress;

import redstone.xmlrpc.XmlRpcClient;

@SuppressWarnings("serial")
public class WP_publisherServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world 5");
		
	     // Get command-line arguments into variables
	      String sXmlRpcURL = "http://www.wptest.masteringwave.com/xmlrpc.php";
	      String sUsername = "test";
	      String sPassword = "qweasd";
	 
	      // Hard-coded blog_ID
	      String blog_ID = "1";
	 

	      // XML-RPC method
	      String sXmlRpcMethod = "metaWeblog.newPost";

	      // We'll hard-code our blog content for now as well
	      String sContent = "[wave id=\"wavesandbox.com!w+DZ1hEVPE%C\"]";
	      String sTitle = "Hello XML-RPC Title";

	      // Create our content struct
	      HashMap hmContent = new HashMap();
	      hmContent.put("title", sTitle);
	      hmContent.put("description", sContent);

	      // Try block
	      try
	      {
	        Wordpress wp = new Wordpress(sUsername, sPassword, sXmlRpcURL);
		      List<Page> recentPosts = wp.getRecentPosts(10);
		      resp.getWriter().println("Here are the ten recent posts:");
		      for (Page page : recentPosts) {
		    	  resp.getWriter().println(page.getPostid() + ":" + page.getTitle());
		      }
		      List<PageDefinition> pages = wp.getPageList();
		      resp.getWriter().println("Here are the pages:");
		      for (PageDefinition pageDefinition : pages) {
		    	  resp.getWriter().println(pageDefinition.getPage_title());
		      }
		      resp.getWriter().println("Posting a test (draft) page from a previous page...");
		      Page recentPost = wp.getRecentPosts(1).get(0);
		      recentPost.setTitle("Test Page");
		      recentPost.setDescription("Test description + DGR");
		      Page newPost = new Page();
		      newPost.setTitle("Wave page");
		      newPost.setDescription(sContent);
		      newPost.setWp_author("The green demo");
		      String result = wp.newPost(newPost, "draft");
		      resp.getWriter().println("new post page id: " + result);
		      
		      resp.getWriter().println("new post page ling: " + newPost.getPermaLink());


	      }

	      // Catch exceptions
	      catch( Exception e )
	      {
	        
	      }
	      
	     
	}
}
